#define _CRT_SECURE_NO_WARNINGS 1
#include"Sort.h"

int PartSort2(int* a, int begin, int end)
{
	int left = begin, right = end;
	int piti = begin;

	int key = a[piti];

	while (left < right)
	{
		while (left < right && a[right] >= key)
		{
			--right;
		}
		a[piti] = a[right];
		piti = right;
		while (left < right && a[left] <= key)
		{
			++left;
		}
		a[piti] = a[left];
		piti = left;
	}
	a[piti] = key;
	return piti;
}

int PartSort1(int* a, int begin, int end)
{
	int left = begin, right = end;
	int keyi = left;

	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
		{
			--right;
		}
		while (left < right && a[left] <= a[keyi])
		{
			++left;
		}
		Swap(&a[left], &a[right]);
	}
	Swap(&a[keyi], &a[left]);
	return left;
}

void QuickSort(int* a, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = PartSort1(a, begin, end);
	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi + 1, end);
}


